package haoji_juc;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;


/**
 * @program: Dome
 * @description:
 * @className: TestArraylist
 * @author: rjt99999
 * @version: 1.0.0
 * @create: 2020-08-03 15:45
 **/
public class TestArraylist {


    /*
    集合并发处理
    并发容器类  --1.8之后改cas算法（取代分段锁）-- ConcurrentHashMap、ConcurrentSkipListMap、ConcurrentSkipListSet、 CopyOnWriteArrayList
    CopyOnWriteArrayList（arraylist） -添加操作不适合，适合读取


    opyOnWriteArrayList/CopyOnWriteArraySet : “写入并复制”
 * 注意：添加操作多时，效率低，因为每次添加时都会进行复制，开销非常的大。并发迭代操作多时可以选择。
     */

    public static void main(String[] args) {

        HelloThread helloThread = new HelloThread();

        for (int i = 0; i < 10; i++) {

            new Thread(helloThread).start();

        }


    }


}

class HelloThread implements Runnable {


 //   private static List<String> list = Collections.synchronizedList(new ArrayList<String>());

 private static CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();


    static {

        Collections.addAll(list, "1", "2", "3");
    }

    @Override
    public void run() {


//        list.stream().forEach((lis) -> {
//            System.out.println(list);
//
//        });


        Iterator<String> it = list.iterator();

        while(it.hasNext()){
            System.out.println(it.next());
            //加数据
            list.add("AA");
        }

    }
}
